package edu.jptest.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import edu.jptest.bean.Answer;
import edu.jptest.util.GetDataBaseConnection;

public class AnswerDAO {
	// 插入答案
	public boolean insertAnswer(Answer answer) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		PreparedStatement pstm = null;
		try {
			String sql = "insert into answer(option_a,option_b,option_c,option_d,option_e,option_f,option_g,option_h,option_i,option_j,option_k,answer,option_count) values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
			pstm = connection.prepareStatement(sql);
			pstm.setString(1, answer.getOptionA());
			pstm.setString(2, answer.getOptionB());
			pstm.setString(3, answer.getOptionC());
			pstm.setString(4, answer.getOptionD());
			pstm.setString(5, answer.getOptionE());
			pstm.setString(6, answer.getOptionF());
			pstm.setString(7, answer.getOptionG());
			pstm.setString(8, answer.getOptionH());
			pstm.setString(9, answer.getOptionI());
			pstm.setString(10, answer.getOptionJ());
			pstm.setString(11, answer.getOptionK());
			pstm.setString(12, answer.getAnswer());
			pstm.setInt(13, answer.getOptionCount());
			int result = pstm.executeUpdate();
			if (result == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
		}
		return false;
	}

	// 删除答案
	public boolean deleteAnswer(int answerId) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		PreparedStatement pstm = null;
		try {
			String sql = "delete from answer where answer_id=?";
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, answerId);
			int result = pstm.executeUpdate();
			if (result == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
		}
		return false;
	}

	// 更该答案
	public boolean updateAnswer(Answer answer) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		PreparedStatement pstm = null;
		try {
			String sql = "update answer set option_a=?,option_b=?,option_c=?,option_d=?,option_e=?,option_f=?,option_g=?,option_h=?,option_i=?,option_j=?,option_k=?,answer=?,option_count=? where answer_id=?";
			pstm = connection.prepareStatement(sql);
			pstm.setString(1, answer.getOptionA());
			pstm.setString(2, answer.getOptionB());
			pstm.setString(3, answer.getOptionC());
			pstm.setString(4, answer.getOptionD());
			pstm.setString(5, answer.getOptionE());
			pstm.setString(6, answer.getOptionF());
			pstm.setString(7, answer.getOptionG());
			pstm.setString(8, answer.getOptionH());
			pstm.setString(9, answer.getOptionI());
			pstm.setString(10, answer.getOptionJ());
			pstm.setString(11, answer.getOptionK());
			pstm.setString(12, answer.getAnswer());
			pstm.setInt(13, answer.getOptionCount());
			pstm.setInt(14, answer.getAnswerId());
			int result = pstm.executeUpdate();
			if (result == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
		}
		return false;
	}

	// 通过答案id，查询答案对象
	public Answer getAnswerByAnswerId(int answer_id) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		Answer answer = new Answer();
		PreparedStatement pstm = null;
		try {
			String sql = "select * from answer where answer_id=?";
			pstm = connection.prepareStatement(sql);
			pstm.setInt(1, answer_id);
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				answer.setAnswerId(resultSet.getInt(1));
				answer.setOptionA(resultSet.getString(2));
				answer.setOptionB(resultSet.getString(3));
				answer.setOptionC(resultSet.getString(4));
				answer.setOptionD(resultSet.getString(5));
				answer.setOptionE(resultSet.getString(6));
				answer.setOptionF(resultSet.getString(7));
				answer.setOptionG(resultSet.getString(8));
				answer.setOptionH(resultSet.getString(9));
				answer.setOptionI(resultSet.getString(10));
				answer.setOptionG(resultSet.getString(11));
				answer.setOptionK(resultSet.getString(12));
				answer.setAnswer(resultSet.getString(13));
				answer.setOptionCount(resultSet.getInt(14));
				return answer;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return new Answer();
	}

	// 通过答案内容，查询答案ID
	public int getAnswerIdByAnswer(Answer answer) {
		GetDataBaseConnection getDataBaseConnection = new GetDataBaseConnection();
		Connection connection = getDataBaseConnection.getConnection();
		ResultSet resultSet = null;
		PreparedStatement pstm = null;
		try {
			String sql = "select answer_id from answer where option_a=? and option_b=? and option_c=? and option_d=? and answer=? and option_count=?";
			pstm = connection.prepareStatement(sql);
			pstm.setString(1, answer.getOptionA());
			pstm.setString(2, answer.getOptionB());
			pstm.setString(3, answer.getOptionC());
			pstm.setString(4, answer.getOptionD());
			pstm.setString(5, answer.getAnswer());
			pstm.setInt(6, answer.getOptionCount());
			resultSet = pstm.executeQuery();
			while (resultSet.next()) {
				int answerId = resultSet.getInt(1);
				return answerId;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					;
				}
			connection = null;
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					;
				}
			}
			pstm = null;
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					;
				}
			}
			resultSet = null;
		}
		return 0;
	}

}
